package com.hk.Mgr.web.controller;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.CityReq;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.entity.City;
import com.hk.commons.entity.Province;
import com.hk.commons.entity.ProvinceCityResponse;
import com.hk.service.sys.ICity;
import com.hk.service.sys.IProvince;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lemon 用户实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/locationCtrl")
public class LocationCtrl extends BaseCtrl {

	private Logger logger = LoggerFactory.getLogger(LocationCtrl.class);

	@Autowired
	private IProvince proService;

	 
	
	@Autowired
	private ICity cityService;
 
	 
	 
	/**
	 * 获取全部的省份列表
	 * @param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getProList", method = RequestMethod.GET)
	public ResponseBase<List<Province>> getProList() {
		ResponseBase<List<Province>> resp = new ResponseBase<List<Province>>();
		
		List<Province> respList = new ArrayList<Province>();
		
		HqlFilter hqlFilter = new HqlFilter();
		
		try{
			respList = proService.findByFilter(hqlFilter);
			resp.setSize(respList.size());
			resp.setResult(respList);
			resp.setResultCode(StatusCode.SUCCESS.key());
			 
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}

		return resp;
	}
	
	/**
	 * 获取全部的省份列表
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getCityListByProCode", method = RequestMethod.POST)
	public ResponseBase<List<City>> getCityListByProCode(@RequestBody CityReq req) {
		ResponseBase<List<City>> resp = new ResponseBase<List<City>>();
		
		List<City> respList = new ArrayList<City>();
		
		HqlFilter hqlFilter = new HqlFilter();
		
		logger.info("proCode = " + req.getProCode());
		
		if( req.getProCode() != null){
			logger.info("proCode = " + req.getProCode());
			hqlFilter.addEQFilter("proCode", req.getProCode());
		}else{
			logger.info("proCode is null");
		}
		
		try{
			respList = cityService.findByFilter(hqlFilter);
			resp.setSize(respList.size());
			resp.setResult(respList);
			resp.setResultCode(StatusCode.SUCCESS.key());
			 
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}

		return resp;
	}

	/**
	 * 3-获取所有省份及省级下的地级市
	 */
	@ResponseBody
	@RequestMapping(value = "/getProAndCityList", method = RequestMethod.GET)
	public ResponseBase<List<ProvinceCityResponse>> getProAndCityList() {
		logger.info("====>/getProAndCityList");
		ResponseBase<List<ProvinceCityResponse>> resp = new ResponseBase<List<ProvinceCityResponse>>();
		List<ProvinceCityResponse> pcrList = new ArrayList<ProvinceCityResponse>();

		List<Province> respList = new ArrayList<Province>();
		HqlFilter hqlFilter = new HqlFilter();
		try{
			respList = proService.findByFilter(hqlFilter);//获取所属省

			for (int i = 0; i < respList.size() ; i++) {
				ProvinceCityResponse pcr = new ProvinceCityResponse();
				Integer proCode = respList.get(i).getProCode();
				HqlFilter hqlCityFilter = new HqlFilter();

				pcr.setProvinceId(respList.get(i).getId());
				pcr.setProCode(respList.get(i).getProCode());
				pcr.setProName(respList.get(i).getProName());

				if( proCode != null){
					hqlCityFilter.addEQFilter("proCode", proCode);
					List<City> cityList = cityService.findByFilter(hqlCityFilter);

					pcr.setBelongCity(JSONArray.fromObject(cityList));
					pcrList.add(pcr);
				}else{
					logger.info("proCode is null");
					pcr.setBelongCity(null);
				}

			}

			resp.setSize(pcrList.size());
			resp.setResult(pcrList);
			resp.setResultCode(StatusCode.SUCCESS.key());

		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}

		return resp;
	}
	 
}
